package net.vkits.controller;

import net.vkits.anjiamiao.application.ComplainApplicationService;
import net.vkits.anjiamiao.domain.model.Complain;
import net.vkits.common.Utils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * Created by ethan-liu on 2016/12/30.
 */
@Controller
@RequestMapping("/appeals")
public class AppealController {
    private static final Logger logger = LoggerFactory.getLogger(AppealController.class);

    @Resource
    private ComplainApplicationService complainApplicationService;

    @RequiresPermissions("customers:view")
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView appeals() {
        String username = Utils.getUsername(SecurityUtils.getSubject()).orElse("Null");
        ModelMap modelMap = new ModelMap();
        modelMap.put("treeview", "customer");
        modelMap.put("sidebar", "appeals");
        modelMap.put("currentUser",username);
        return new ModelAndView("/appeals", modelMap);
    }

    @RequiresPermissions("customers:view")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Page<Complain> getContracts(@PageableDefault(value = 10, sort = {"id"}, direction = Sort.Direction.DESC)Pageable pageable) {
        Page<Complain> complains = complainApplicationService.getComplains(pageable);
        logger.info("size is: {}", complains.getTotalElements());
        return complains;
    }

    @RequiresPermissions("customers:modify")
    @RequestMapping(value ="/{id}", method = RequestMethod.GET)
    public ModelAndView customer(@PathVariable String id) {
        String username = Utils.getUsername(SecurityUtils.getSubject()).orElse("Null");
        Complain complain = complainApplicationService.findById(id);
        ModelMap modelMap = new ModelMap();
        modelMap.put("complain", complain);
        modelMap.put("treeview", "customer");
        modelMap.put("sidebar", "appeals");
        modelMap.put("currentUser", username);
        return new ModelAndView("/appeal_backend", modelMap);
    }

    @RequiresPermissions("customers:modify")
    @RequestMapping(value ="/update", method = RequestMethod.POST)
    public String contractUpdate(@RequestParam(value = "id", required = true) String id,
                                 @RequestParam(value = "status", required = true) String status) {
        logger.info("id: {}", id);
        complainApplicationService.changeStatus(id, status);
        return "redirect:/appeals";
    }
}
